//
//  YZBaseAutoVC.m
//  VET
//
//  Created by Lam BG on 2018/7/12.
//  Copyright © 2018年 YZ. All rights reserved.
//

#import "YZBaseAutoVC.h"

// auto model
#import "YZAutoVCModel.h"
#import "YZAutoHFModel.h"
#import "YZAutoRowModel.h"
#import "YZAutoSectionModel.h"

#import "YZAutoTipsHF.h"
#import "YZJsonFileLoader.h"

// 第三方
#import "MJExtension.h"

@interface YZBaseAutoVC () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, weak) UITableView *tableView;

@end

static NSString* headID = @"kAutoTipsHeadID";

@implementation YZBaseAutoVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = self.avcModel.vcTitle;
    [self setupTableView];
    [self loadData];
    [self.tableView reloadData];
}

- (void)setupTableView {
    
    // tableView 设置
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;             // 将原点移动到navigationBar下面去了
    self.tableView.estimatedRowHeight = 37.0;                 // 估计cell的高度
    self.tableView.rowHeight = UITableViewAutomaticDimension; // 行高自适应

    
    self.tableView.tableFooterView = [UIView new];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

}

- (void)loadData {
    
    // 填充数据
    NSString *action = self.avcModel.vcAction;

    NSAssert(action.length != 0, @"数据出错 请检查");
    
    NSArray *jsonArray = [self viewJson:action];
    
    NSAssert(jsonArray.count > 0, @"数据出错 请检查");
    
    NSArray *array = [YZAutoSectionModel mj_objectArrayWithKeyValuesArray:jsonArray];
    [self.data removeAllObjects];
    [self.data addObjectsFromArray:array];
}

#pragma mark - table view delegate

// section nums
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return self.data.count;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    YZAutoSectionModel *sectionModel = self.data[section];
    YZAutoHFModel *header = sectionModel.header;
    
    if (header.type == AutoHFTypeMinFont) {

        YZAutoTipsHF *headerView = (YZAutoTipsHF*)[[[NSBundle mainBundle] loadNibNamed:@"YZAutoTipsHF" owner:self options:nil] firstObject];
        [headerView setTitle:header.info];

        return headerView;
    }
    
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    YZAutoSectionModel *sectionModel = self.data[section];
    YZAutoHFModel *header = sectionModel.header;
    
    if (header.type == AutoHFTypeDefaultHeiht) return 12;
    if (header.type == AutoHFTypeMinFont) return 36;
        
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    
    YZAutoSectionModel *sectionModel = self.data[section];
    YZAutoHFModel *footer = sectionModel.footer;
    
    if (footer.type == AutoHFTypeDefaultHeiht) return 12;
    
    return 0.01;
}


- (void)tableView:(UITableView *)tableView willDisplayFooterView:(UIView *)view forSection:(NSInteger)section {
    
    view.tintColor = [UIColor clearColor];
}

- (void)tableView:(UITableView *)tableView willDisplayHeaderView:(UIView *)view forSection:(NSInteger)section {
    
    view.tintColor = [UIColor clearColor];
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
}

#pragma mark - 子类实现

- (NSArray*)viewJson:(NSString*)fileName {return nil;}

// rows in section nums
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return nil;
}

#pragma mark - getter

- (NSMutableArray *)data {
    
    if (_data == nil) {
        
        _data = [NSMutableArray array];
    }
    
    return _data;
}

@end















